package com.xmb.wechat.view.wechat.chat;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.adapter.WechatLastMsgAdapter;
import com.xmb.wechat.application.WechatStartApplication;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.bean.WechatLastMsgBean;
import com.xmb.wechat.bean.WechatLastMsgBean_;
import com.xmb.wechat.bean.WechatMsgBean;
import com.xmb.wechat.bean.WechatMsgBean_;
import com.xmb.wechat.definterface.HintListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/8 12:00
 */
public class WechatDanliaoActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemLongClickListener {

    public static final int REQUEST_CODE_ADD_DANLIAO  = 777;
    public static final int REQUEST_CODE_EDIT_DANLIAO = 778;

    @BindView(R2.id.ll_hint)
    LinearLayout mLlHint;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;

    private WechatLastMsgAdapter   mAdapter;
    private Box<WechatLastMsgBean> mBox;

    public WechatDanliaoActivity() {
        super(R.layout.activity_danliao);
    }

    @Override
    protected void initView() {
        mAdapter = new WechatLastMsgAdapter(WechatDanliaoActivity.this, R.layout.wechat_last_msg_item, null);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(WechatDanliaoActivity.this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemLongClickListener(this);
        mBox = WechatStartApplication.getBoxStore().boxFor(WechatLastMsgBean.class);
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                WechatDanliaoActivity.this.finish();
            }
        });
    }

    @OnClick({R2.id.ll_hint, R2.id.btn_add})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_hint) {
            mLlHint.setVisibility(View.GONE);
        } else if (id == R.id.btn_add) {
            startActivityForResult(new Intent(WechatDanliaoActivity.this, WechatContactsActivity.class), REQUEST_CODE_ADD_DANLIAO);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        ArrayList<WechatLastMsgBean> datas = (ArrayList<WechatLastMsgBean>) mBox.query().equal(WechatLastMsgBean_.isRoomChat, false)
                .orderDesc(WechatLastMsgBean_.time).build().find();
        if (datas.size() > 0) {
            mAdapter.getData().clear();
            mAdapter.addData(datas);
            mAdapter.notifyDataSetChanged();
            showEmptyView(false);
            mLlHint.setVisibility(View.VISIBLE);
        } else {
            showEmptyView(true);
            mLlHint.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        WechatLastMsgBean wechatLastMsgBean = (WechatLastMsgBean) adapter.getData().get(position);
        WechatContactBean contact = WechatStartApplication.getBoxStore().boxFor(WechatContactBean.class).get(wechatLastMsgBean.getTalkerID());
        WechatDanliaoDetailActivity.start(WechatDanliaoActivity.this, contact);
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        final WechatLastMsgBean wechatLastMsgBean = (WechatLastMsgBean) adapter.getData().get(position);
        String[] items = new String[]{"编辑", "删除"};
        new AlertDialog.Builder(WechatDanliaoActivity.this).setItems(items,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                WechatDanliaoEditActivity.start4result(WechatDanliaoActivity.this, wechatLastMsgBean, REQUEST_CODE_EDIT_DANLIAO);
                                break;
                            case 1:
                                mBox.remove(wechatLastMsgBean);
                                //删除聊天消息
                                Box<WechatMsgBean> wechatMsgBeanBox = WechatStartApplication.getBoxStore(WechatDanliaoActivity.this).boxFor(WechatMsgBean.class);
                                List<WechatMsgBean> wechatMsgBeans = wechatMsgBeanBox.query().equal(WechatMsgBean_.talkerID, wechatLastMsgBean.getTalkerID()).build().find();
                                wechatMsgBeanBox.remove(wechatMsgBeans);
                                refresh();
                                break;
                        }
                    }
                }).setNegativeButton("取消", null).show();
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_DANLIAO || requestCode == REQUEST_CODE_EDIT_DANLIAO)
            refresh();
    }
}
